let canvas = document.getElementsByTagName("canvas")[0];

canvas.onclick = function() {
	let x = Math.round(window.event.offsetY / 50) - 1;
	let y = Math.round(window.event.offsetX / 50) - 1;
	console.log([x, y]);

	//坐标范围不对
	if (x < 0 || x > 9 || y < 0 || y > 8) {
		return;
	}

	let arr = piecesToArray(chess.pieces);
	//还没选棋子就点空地方，无效点击
	if (arr[x][y] == 0 && chess.selects.length != 1) {
		return;
	}

	if (chess.selects.length == 0 || chess.selects.length == 2) {
		chess.selects = [{
			x: x,
			y: y
		}];
	} else if (chess.selects.length == 1) {
		if (chess.selects[0].x == x && chess.selects[0].y == y) {
			chess.selects = [];
		} else {
			chess.selects.push({
				x: x,
				y: y
			});
			let x1 = chess.selects[0].x,
				y1 = chess.selects[0].y,
				x2 = x,
				y2 = y;
			arr[x2][y2] = arr[x1][y1];
			arr[x1][y1] = 0;
			chess.pieces = arrToPieces(arr);

			chess.history.push([x1, y1, x2, y2]);
		}
	}

	chess.show();
};
